import { defineConfig ,loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'node:path'
import { viteMockServe } from 'vite-plugin-mock'
import cesium from 'vite-plugin-cesium'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig(({ command, mode }) => {
   //获取各种环境下的对应的变量
 let env = loadEnv(mode, process.cwd())
  return {
    optimizeDeps: {
      include: ['echarts']
    },
    base: './',
    plugins: [
      vue(),
      cesium(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        enable: command === 'serve',
        mockPath: './mock', // 明确指定mock文件目录
        logger: true, // 启用日志
      }),
    ],
    //scss全局变量一个配置
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/styles/variable.scss";',
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve("./src")
      }
    },
    //代理跨域
    server: {
      port: 5173, // 固定开发服务器端口
      proxy: {
        '/api': {
          target: 'http://localhost:5173', // 指向开发服务器
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '/api') // 保持/api前缀
        }
      }
    }
  }
})
